/*
  ID: nemokin1
  PROG: transform
  LANG: C++
*/

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
 
using namespace std;
 
#define REP(i,n) for(int i=0; i<(n); ++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=b;--i)
#define FOREACH(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define ALL(c) (c).begin(),(c).end()
 
#define debug(x) cerr<< #x << " = " << (x) << "\n";
#define debugv(v) cerr << #v << " = "; FOREACH(it,v) cerr << *it <<","; cerr << "\n";
#define SIZE(a)  (int(a.size()))
 
template<typename T> inline void checkMax(T& a, T b) {if (b>a) a=b;}
template<typename T> inline void checkMin(T& a, T b) {if (b<a) a=b;}
 
typedef long long LL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef long double ld;
const int INF = 1000000000;


typedef VS (*func)(VS from);
string T(7,'0');


VS rotation90(VS from)
{
    //cerr<<"rotation90\n";
    
    int N = SIZE(from);
    VS ret(from);
    REP(i,N){
	REP(j,N){
	    ret[i][j] = from[N-1-j][i];
	}
    }
    //debugv(from);
    //debugv(ret);
    return ret;
}
VS rotation180(VS from)
{
    //cerr<<"rotation180\n";
    
    VS ret = rotation90(from);
    ret = rotation90(ret);
    //debugv(from);
    //debugv(ret);
    return ret;
}
VS rotation270(VS from)
{
    //cerr<<"rotation270\n";
    
    VS ret = rotation180(from);
    ret = rotation90(ret);
    return ret;
}
VS reflect(VS from)
{
    //cerr<<"reflect\n";
    
    int N = SIZE(from);
    VS ret = from;
    REP(i,N) REP(j,N) ret[i][j] = from[i][N-1-j];
    //debugv(from);
    //debugv(ret);
    return ret;
}
VS reflect90(VS from)
{
    //cerr<<"reflect90\n";
    
    VS ret = from;
    ret = reflect(from);
    ret = rotation90(ret);
    return ret;
}
VS reflect180(VS from)
{
    cerr<<"reflect180\n";
    
    VS ret;
    ret = reflect(from);
    ret = rotation180(ret);
    //debugv(from);
    //debugv(ret);
    
    return ret;
}
VS reflect270(VS from)
{
    //cerr<<"reflect270\n";
    
    VS ret = from;
    ret = reflect(from);
    ret = rotation270(ret);
    //debugv(from);
    //debugv(ret);
    
    return ret;
}


func listt[] = {rotation90,rotation180,rotation270,reflect,reflect90,reflect180,reflect270};
int test(VS from, VS to)
{
    if(from == to) return 1;
    for(int i = 1; i <= 7; i++){
	for(int j = 6; j > 6-i; j--) T[j] = '1';
	debug(T);
	int cc = 101;
	cerr<<"--------\n";
	do{
	    VS ret = from;
	    debug(T);
	    int _i;
	    int mini = 100;
	    for(_i = 0; _i < 7; _i++){
		if(T[_i] == '1'){
		    //debug(_i);
		    checkMin(mini,_i);
		    ret = (*listt[_i])(ret);
		}
	    }
	    if(ret == to) checkMin(cc,mini);
	}while(next_permutation(ALL(T)));
	if(cc == 101) continue;
	else return cc>=4?4:cc;
    }
    return 6;
}

int main() {
    freopen("transform.in","r",stdin);freopen("transform.out","w",stdout);
    int N;
    cin >> N;
    VS from,to;
    string s;
    REP(i,N){
	cin >> s;
	from.push_back(s);
    }
    REP(i,N){
	cin >> s;
	to.push_back(s);
    }
    //debugv(from);
    //debugv(to);
    int ret;
    ret = test(from,to);
    cout << ret+1 << endl;
    exit(0);
}
